package com.breakfast.fun.ui.other.city;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.breakfast.fun.Constant;
import com.breakfast.fun.MainActivity;
import com.breakfast.fun.adapter.home.CityAdapter;
import com.breakfast.fun.base.BaseActivity;
import com.breakfast.fun.data.entity.home.CityData;
import com.breakfast.fun.ui.home.HomeFragment;
import com.breakfast.fun.util.SharedUtil;
import com.breakfast.fun.view.widget.TitleBarView;
import com.google.gson.Gson;
import com.breakfast.fun.R;

import java.util.List;

import butterknife.Bind;

/**
 * Created by Administrator on 2016/8/22.
 */
public class CityActivity extends BaseActivity<CityPresenter, CityModel> implements CityContract.View {

    @Bind(R.id.tbv_city)
    TitleBarView tbvCity;
    @Bind(R.id.lv_city)
    ListView lvCity;


    @Override
    public int getLayoutId() {
        return R.layout.activity_city;
    }

    @Override
    public void initView() {
        tbvCity.setTitleNoRight("选择城市");
        tbvCity.setBack(this);
    }


    @Override
    public void showCity(List<CityData> cityList) {
        CityAdapter adapter = new CityAdapter(context, cityList);
        lvCity.setAdapter(adapter);
        lvCity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (cityList.get(position).getState() != 2) {
                    Constant.setCityData(cityList.get(position));
                    Gson gson = new Gson();
                    SharedUtil.putString(context, "city", gson.toJson(cityList.get(position)));
                    HomeFragment.isNeedRefresh = true;
                    HomeFragment.isChangeCity = true;
                    HomeFragment.isNeedShowAd = true;
                    Intent intent = new Intent(CityActivity.this, MainActivity.class);
                    startActivity(intent);
                    CityActivity.this.finish();
                } else {
                    return;
                }
            }
        });
    }


}
